package com.user.controller;

import com.sun.jmx.snmp.Timestamp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @Auther: hejianding
 * @Date: 2021/3/12 10:13
 * @Version: 1.0
 */
@RestController
public class Usercontroller {
    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping("/user/{id}")
    public String getUser(@PathVariable("id") Long id) {

        //获取用户微服务实例
        ServiceInstance serviceInstance = discoveryClient.getInstances("USER").get(0);
        //控制台输出微服务被调用时间、端口号等信息
        System.out.println("time: " + new Timestamp(System.currentTimeMillis()) + ", serverId: " + serviceInstance.getServiceId() + ", host:" +
                " " + serviceInstance.getHost()
                + ", port: " + serviceInstance.getPort());
        return "user_id_" + id;
    }
}
